package com.hourses.service.impl;

import com.hourses.mapper.RoleMapper;
import com.hourses.mapper.UserMapper;
import com.hourses.page.Page;
import com.hourses.pojo.Result;
import com.hourses.pojo.Role;
import com.hourses.pojo.User;
import com.hourses.service.UserService;
import com.hourses.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/4/26 16:58
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User selectByCode(String code) {
        return userMapper.selectByCode(code);
    }

    @Override
    public Page selectUserList(User user, Page page) {
        Integer userCount = userMapper.selectTotalUser(user);
        page.setTotalNum(userCount);
        List<User> users = userMapper.selectUserListByPage(user, page);
        page.setResultList(users);
        return page;
    }

    //增加用户
    @Override
    public Result insertUser(User user) {
        //先判断账号是否注册过了
        User dbUserCode = userMapper.selectByCode(user.getUserCode());
        if (dbUserCode!=null){
            //账号已经注册过了
            return Result.err(Result.CODE_ERR_BUSINESS,"账号添加失败!账号已经存在");
        }

        //账号没有注册过
        //先对账号进行加密操作
        String hmacSignPwd = DigestUtil.hmacSign(user.getUserPwd());
        user.setUserPwd(hmacSignPwd);
        Integer count = userMapper.insertUser(user);
        if (count>0){
            //注册成功
            return Result.ok("用户添加成功！");
        }
        //添加失败
        return Result.err(Result.CODE_ERR_BUSINESS,"账号添加失败！请联系管理员");

    }

    @Override
    public Result updateUserState(User user) {
        Integer count=0;
        if (user.getUserState().equals("0")){   //0 未审核 、1 已审核
            //说明点击按钮是要禁用用户
            count = userMapper.updateUserState("0",user.getUserId());
            if(count==1){
                return Result.ok("禁用用户成功！");
            }else {
                return Result.err(Result.CODE_ERR_BUSINESS,"禁用用户失败！请联系管理员");
            }
        }
        if (user.getUserState().equals("1")){
            //说明点击按钮是要启用用户
            count = userMapper.updateUserState("1",user.getUserId());
            if(count==1){
                return Result.ok("启用用户成功！");
            }else {
                return Result.err(Result.CODE_ERR_BUSINESS,"启用用户失败！请联系管理员");
            }
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"启用用户失败！请联系管理员");
    }


    //给用户分配角色
    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional         //添加事务
    public Result setUserRoles(Integer userId, List<String> roles) {
        //先根据userId删除用户的角色
        /**
         * 先判断用户是否有角色，有的话再进行删除，没有的话直接进行下一步
         */
        List<Role> userOldRoles = roleMapper.selectUserRolesByUserId(userId);
        if (userOldRoles!=null&&userOldRoles.size()!=0){
            //不为空，代表用户原本具有某些角色，先进行删除，再进行下一步操作
            int count = roleMapper.deleteUserRolesByUserId(userId);
            //判断是否删除成功
            if (count==0) {
                //删除失败
                return Result.err(Result.CODE_ERR_BUSINESS, "修改失败！请联系管理员..");
            }
        }
        //用户原本没有角色或者原本具有角色，现已删除原有角色，现在给用户赋上新角色
        if (roles!=null) {
            for (String role : roles) {
                //每次取出新角色中的其中一个，根据新角色的名字，获取到新角色的role_id,将其与userId一同插入到user_role表中去
                //1.获取role_id
                int roleId = roleMapper.getRoleIdByRoleName(role);
                //2.将roleId与userId一同插入到user_role表中去
                int insertCount = roleMapper.insertUserRole(roleId, userId);
                if (insertCount == 0) {
                    return Result.err(Result.CODE_ERR_BUSINESS, "修改失败！请联系管理员...");
                }
            }
        }
        return Result.ok("修改成功！");
    }


    /**
     * 修改用户信息为删除状态
     * @param userIds
     * @return
     */
    @Override
    public Result updateIsdeleteByUserIds(List<Integer> userIds) {
        Integer count = userMapper.updateIsdeleteByUserIds(userIds);
        if (count>0){
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败...");
    }

    /**
     * 通过userId修改用户信息(姓名)
     * @param user
     * @return
     */
    @Override
    public Result updateUserNameByUserId(User user) {
        Integer count = userMapper.updateUserNameByUserId(user);
        if (count>0){
            return Result.ok("修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改失败！请联系管理员...");
    }

    @Override
    public Result resetPwd(Integer userId) {
        String newPwd = DigestUtil.hmacSign("123456");
        Integer count = userMapper.resetPwd(userId, newPwd);
        if (count>0){
            return Result.ok("重置密码成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"重置密码失败！请联系管理员...");
    }
}
